//
//  MessageCell.m
//  WisdomCommunity
//
//  Created by Yahaong on 16/7/29.
//  Copyright © 2016年 zonten. All rights reserved.
//

#import "MessageCell.h"
#import "YHMarcros.h"
#import "UITableViewCell+HYBMasonryAutoCellHeight.h"

@interface MessageCell ()
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *descLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIButton *likeButton;
@property (nonatomic, strong) UIButton *sharedButton;
@property (nonatomic, strong) UIButton *commontButton;
@property (nonatomic, strong) JGGView *jggView;
@property (nonatomic, strong) UIView *commentView;
@property (nonatomic, strong) NSIndexPath *indexPath;

@end
@implementation MessageCell



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.headImageView = [[UIImageView alloc] init];
        self.headImageView.backgroundColor = [UIColor whiteColor];
        self.headImageView.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:self.headImageView];
        [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(kGAP);
            make.width.height.mas_equalTo(kAvatar_Size);
        }];
        self.nameLabel = [UILabel new];
        [self.contentView addSubview:self.nameLabel];
        self.nameLabel.textColor = [UIColor colorWithRed:(54/255.0) green:(71/255.0) blue:(121/255.0) alpha:0.9];
        self.nameLabel.preferredMaxLayoutWidth = kScreenWidth - kGAP-kAvatar_Size - 2*kGAP-kGAP;
        self.nameLabel.numberOfLines = 0;
        //        self.nameLabel.displaysAsynchronously = YES;
        self.nameLabel.font = [UIFont systemFontOfSize:14.0];
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.headImageView.mas_right).offset(10);
            make.top.mas_equalTo(self.headImageView);
            make.right.mas_equalTo(-kGAP);
        }];
        // desc
        self.descLabel = [UILabel new];
        [self.contentView addSubview:self.descLabel];
        self.descLabel.preferredMaxLayoutWidth = kScreenWidth - kGAP-kAvatar_Size ;
        self.descLabel.numberOfLines = 0;
        self.descLabel.font = [UIFont systemFontOfSize:14.0];
        [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self.nameLabel);
            make.top.mas_equalTo(self.nameLabel.bottom).offset(10);
        }];
        self.jggView = [JGGView new];
        [self.contentView addSubview:self.jggView];
        [self.jggView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.descLabel);
            make.top.mas_equalTo(self.descLabel.mas_bottom).offset(kGAP);
        }];
        self.timeLabel = [UILabel new];
        self.timeLabel.text = @"昨天 20:32";
        self.timeLabel.font = [UIFont systemFontOfSize:14.0];
        self.timeLabel.textColor = RGBColor(102, 102, 102);
        [self.contentView addSubview:self.timeLabel];
        [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.jggView);
            make.top.mas_equalTo(self.jggView.mas_bottom).offset(kGAP);
        }];
        
        self.sharedButton = [[UIButton alloc]init];
        [self.sharedButton setImage:[UIImage imageNamed:@"邻里互动5_"] forState:UIControlStateNormal];
        [self.contentView addSubview:self.sharedButton];
        [self.sharedButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.timeLabel);
            make.trailing.equalTo(self.contentView).offset(-8);
        }];

        self.commontButton = [[UIButton alloc]init];
        [self.commontButton setImage:[UIImage imageNamed:@"邻里互动4_"] forState:UIControlStateNormal];
        [self.commontButton setTitle:@"1" forState:UIControlStateNormal];
        [self.contentView addSubview:self.commontButton];
        [self.commontButton setTitleColor:RGBColor(102, 102, 102) forState:UIControlStateNormal];
        self.commontButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.commontButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.timeLabel);
            make.trailing.equalTo(self.sharedButton.leading).offset(-8);
        }];
        
        self.likeButton = [[UIButton alloc]init];
        [self.likeButton setImage:[UIImage imageNamed:@"邻里互动3_1"] forState:UIControlStateNormal];
        [self.likeButton setTitle:@"1" forState:UIControlStateNormal];
         self.likeButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.likeButton];
        [self.likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.timeLabel);
            make.trailing.equalTo(self.commontButton.leading).offset(-8);
        }];
        [self.likeButton setTitleColor:RGBColor(102, 102, 102) forState:UIControlStateNormal];
    }
    return self;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)configCellWithModel:(MessageModel *)model indexPath:(NSIndexPath *)indexPath
{
    self.indexPath = indexPath;
     self.nameLabel.text = model.userName;
    self.headImageView.image = [UIImage imageNamed:model.photo];
    self.descLabel.text = model.message;
    
    
    CGFloat jjg_height = 0.0;
    CGFloat jjg_width = 0.0;
    if (model.messageSmallPics.count>0&&model.messageSmallPics.count<=3) {
        jjg_height = [JGGView imageHeight];
        jjg_width  = (model.messageSmallPics.count)*([JGGView imageWidth]+kJGG_GAP)-kJGG_GAP;
    }else if (model.messageSmallPics.count>3&&model.messageSmallPics.count<=6){
        jjg_height = 2*([JGGView imageHeight]+kJGG_GAP)-kJGG_GAP;
        jjg_width  = 3*([JGGView imageWidth]+kJGG_GAP)-kJGG_GAP;
    }else  if (model.messageSmallPics.count>6&&model.messageSmallPics.count<=9){
        jjg_height = 3*([JGGView imageHeight]+kJGG_GAP)-kJGG_GAP;
        jjg_width  = 3*([JGGView imageWidth]+kJGG_GAP)-kJGG_GAP;
    }
    ///解决图片复用问题
    [self.jggView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    ///布局九宫格
    [self.jggView JGGView:self.jggView DataSource:model.messageSmallPics completeBlock:^(NSInteger index, NSArray *dataSource,NSIndexPath *indexpath) {
       
    }];
    [self.jggView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.descLabel);
        make.top.mas_equalTo(self.descLabel.mas_bottom).offset(kJGG_GAP);
        make.size.mas_equalTo(CGSizeMake(jjg_width, jjg_height));
    }];
    self.hyb_lastViewInCell = self.timeLabel;
    self.hyb_bottomOffsetToCell = 10.0;
}
@end
